import { WECHAT, HTML5, BYTEDANCE } from 'cc/env';
import { updateUserGoldCoin, updateUserHeader, updateUserGameNum, updateUserPlayLevel } from '../services/UserInfo';
import { Databus } from '../core/Databus';
import { ResourceManager } from '../framework/ResourceManager';
const databus = Databus.Instance();
// 不同平台登陆方式不一样，这里用一个类封装起来
export class UserInfo {
  private userInfoButton: any = null;
  public createUserInfo() {
    if (databus.hasAuthHeaderName()) {
      console.log('has auth header name ');
      return;
    }
    if (WECHAT) {
      this.wxCreateUserInfo();
    } else if (BYTEDANCE) {
      this.byteDanceCreateUserInfo();
    }
  }

  public showCreateButton() {
    if (this.userInfoButton) {
      this.userInfoButton.show();
    }
  }
  public hideCreateButton() {
    if (this.userInfoButton) {
      this.userInfoButton.hide();
    }
  }

  public wxCreateUserInfo() {
    wx.getUserProfile({
      desc: '用于对局展示头像和昵称',
      success(res: any) {
        console.log(`getUserInfo 调用成功`, res.userInfo, res.encryptedData, res.iv, res.signature);
        if (res.hasOwnProperty('userInfo')) {
          // 更新用户头像和昵称
          databus.selfInfo.setUserHeaderInfo(res.userInfo.nickName, res.userInfo.avatarUrl);
          updateUserHeader(databus.selfInfo.userId, res.userInfo.nickName, res.userInfo.avatarUrl);
  
          ResourceManager.Instance().loadImageSpriteFrame(
            ResourceManager.SelfUserHeaderImage,
            databus.selfInfo.avatarUrl,
            ResourceManager.Instance().getImageExt(databus.selfInfo.avatarUrl),
          );
        }
        console.log('wx userInfo ', res);
      },
      fail(res: any) {
        console.log(`wx getUserInfo 调用失败`, res.errMsg);
      },
    });
  }

  public byteDanceCreateUserInfo() {
    // 获取用户信息
    tt.getUserInfo({
      success(res: any) {
        console.log(`getUserInfo 调用成功`, res.userInfo, res.encryptedData, res.iv, res.signature);
        if (res.hasOwnProperty('userInfo')) {
          // 更新用户头像和昵称
          databus.selfInfo.setUserHeaderInfo(res.userInfo.nickName, res.userInfo.avatarUrl);
          updateUserHeader(databus.selfInfo.userId, res.userInfo.nickName, res.userInfo.avatarUrl);
  
          ResourceManager.Instance().loadImageSpriteFrame(
            ResourceManager.SelfUserHeaderImage,
            databus.selfInfo.avatarUrl,
            ResourceManager.Instance().getImageExt(databus.selfInfo.avatarUrl),
          );
        }
        console.log('bytedance userInfo ', res);
      },
      fail(res: any) {
        console.log(`getUserInfo 调用失败`, res.errMsg);
      },
    });
  }

  public updateUserGoldCoin(userId: string, gc: number, coin: number) {
    if (HTML5) {
      return;
    }
    updateUserGoldCoin(userId, gc, coin);
  }
  public updateUserGameNum(userId: string, diffNum: number, winNum: number, loseNum: number, tieNum: number) {
    if (HTML5) {
      return;
    }
    updateUserGameNum(userId, diffNum, winNum, loseNum, tieNum);
  }

  public updateUserPlayLevel(userId: string, playLevel: number, diffLevel: number) {
    if (HTML5) {
      return;
    }
    updateUserPlayLevel(userId, playLevel, diffLevel);
  }
}